package com.dayuanit.pokergame;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class GameUtil {

    private static final Map<Integer, String> colorMap = new HashMap<>();
    private static final Map<Integer, String> valueMap = new HashMap<>();
    private static final Map<Integer, Integer> weightMap = new HashMap<>();

    private static int indexPoker = 0;

    static {
        colorMap.put(0, "方片");
        colorMap.put(1, "红桃");
        colorMap.put(2, "梅花");
        colorMap.put(3, "黑桃");

        valueMap.put(0, "A");
        valueMap.put(1, "2");
        valueMap.put(2, "3");
        valueMap.put(3, "4");
        valueMap.put(4, "5");
        valueMap.put(5, "6");
        valueMap.put(6, "7");
        valueMap.put(7, "8");
        valueMap.put(8, "9");
        valueMap.put(9, "10");
        valueMap.put(10, "J");
        valueMap.put(11, "Q");
        valueMap.put(12, "K");

        weightMap.put(0, 14);
        weightMap.put(1, 2);
        weightMap.put(2, 3);
        weightMap.put(3, 4);
        weightMap.put(4, 5);
        weightMap.put(5, 6);
        weightMap.put(6, 7);
        weightMap.put(7, 8);
        weightMap.put(8, 9);
        weightMap.put(9, 10);
        weightMap.put(10, 11);
        weightMap.put(11, 12);
        weightMap.put(12, 13);
    }

    public static Poker[] createaPoker() {
        Poker[] pokers = new Poker[52];
        int index = 0;
        //外层循环是点数
        for (int i=0; i<13; i++) {
            //内层循环是花色
            for (int j=0; j<4; j++) {
                pokers[index ++] = new Poker(valueMap.get(i), colorMap.get(j), weightMap.get(i));
//                pokers[++ index] = new Poker(valueMap.get(i), colorMap.get(j));//error 会出现数组越界的问题
            }
        }

        return pokers;
    }

    public static void shuffle(Poker[] pokers) {
        Random random = new Random();
        for (int i=pokers.length; i>1; i--) {
            swap(pokers, i-1, random.nextInt(i));
        }
    }

    //交换两个元素
    private static void swap(Poker[] pokers, int i, int j) {
        Poker temp = pokers[i];
        pokers[i] = pokers[j];
        pokers[j] = temp;
    }

    //发牌
    public static void dealPork(Poker[] pokers, Person person) {
        person.setPokers(pokers[indexPoker ++], pokers[indexPoker ++]);
    }

    public static void sum(Person person, int grade) {
        person.setGrade(person.getGrade() + grade);
    }
}
